﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_SectionBalance : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		CheckPageState();
		if (!IsPostBack)
		{
			LoadData();
			LoadParameters();
		}
    }

	private void CheckPageState()
	{
		//if not ValidateSecurity() then
		//	Session("Error") = "Nu aveti suficiente drepturi pentru pagina ceruta."
		//	Response.Redirect Application("AbsPath") & "/main.asp"
		//end if

		//if not ValidatePage() then
		//	Session("Error") = "Pagina pe care ati cerut-o nu va este accesibila."
		//	Response.Redirect Application("AbsPath") & "/default.asp"
		//end if

		//if not OpenDBConnection() then
		//	Session("Error") = "Conexiunea la baza de date nu poate fi deschisa.<br>" & Session("Error")
		//	Response.Redirect Application("AbsPath") & "/noAccess.asp"
		//end if
	}

	private void LoadParameters()
	{
		if (Session["REGISTER_SD"] != null)
			strSD.Text = ((DateTime)Session["REGISTER_SD"]).ToString(CalendarExtenderStart.Format);
		if (Session["REGISTER_SH"] != null)
			strSH.Text = Session["REGISTER_SH"].ToString();
		if (Session["REGISTER_ED"] != null)
			strED.Text = ((DateTime)Session["REGISTER_ED"]).ToString(CalendarExtenderStart.Format);
		if (Session["REGISTER_EH"] != null)
			strEH.Text = Session["REGISTER_EH"].ToString();
		if (Session["REGISTER_SECTION"] != null)
			strSection.SelectedValue = Session["REGISTER_SECTION"].ToString();

		DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
		if (strSD.Text.Length == 0)
		{
			strSD.Text = startDate.ToString(CalendarExtenderStart.Format);
			if (Session["SH_Statistica_BAL"] != null)
				strSH.Text = Session["SH_Statistica_BAL"].ToString(); // "14:00"
		}
		if (strED.Text.Length == 0)
		{
			strED.Text = startDate.AddMonths(1).AddDays(-1).ToString(CalendarExtenderStart.Format);
			if (Session["EH_Statistica_BAL"] != null)
				strEH.Text = Session["EH_Statistica_BAL"].ToString(); // "13:59"
		}

	//if strSection&"" = "" then
	//    strSection = Session("Section")
	//end if
		strSH.Text = "00:00";
		strEH.Text = "23:59";
	}

	private void LoadData()
	{
		string oldSql = "select distinct s.* from SECTIONS s ";
		//if (checkSecLevel(SEC_ADMIN))
			oldSql += " where Clinic = 1";
		//else
		//	oldSql += @"inner join vUserSections gu on s.Code=gu.SectionCode
		//		where GroupID in (" + Session["SQLUserGroups"] + ")";
		oldSql += @"  AND s.Active=1 AND Therapy = 0
			order by s.Name";
		System.Data.DataTable dTable = Hipocrate.DB.DBConnection.GetData("Sections_Code",
			new string[] { "@sectionCode" },
			new object[] { null }).Tables[0];
		foreach (System.Data.DataColumn c in dTable.Columns) c.AllowDBNull = true;
		System.Data.DataRow dRowAll = dTable.NewRow();
		dRowAll["code"] = "*";
		dRowAll["name"] = "Toate";
		dTable.Rows.InsertAt(dRowAll, 0);
		foreach (System.Data.DataRow dRow in dTable.Rows)
			dRowAll["code"] = Server.HtmlEncode(dRowAll["code"].ToString());
		strSection.DataSource = dTable;
		strSection.DataBind();
	}

	private bool ValidateInput()
	{
		bool inputState = true;
		string errors = "";
		DateTime dummy;
		if (DateTime.TryParse(strSD.Text, out dummy))
		{
			if (!strSD.Text.Equals(dummy.ToString(CalendarExtenderStart.Format)))
			{
				strSD.Text = dummy.ToString(CalendarExtenderStart.Format);
				strSD.BackColor = System.Drawing.Color.Yellow;
				errors += "Data de inceput nu a fost introdusa in formatul standard. Valoarea a fost modificata corespunzator; este inca corecta?";
				inputState = false;
			}
			else
				strSD.BackColor = System.Drawing.Color.White;
		}
		else
		{
			strSD.BackColor = System.Drawing.Color.Pink;
			errors += "Data de inceput nu a putut fi preluata!"; //"Valoarea este invalida."?
			inputState = false;
		}
		if (DateTime.TryParse(strSH.Text, out dummy))
		{
			if (!strSH.Text.Equals(dummy.ToString("HH:mm")))
			{
				strSH.Text = dummy.ToString("HH:mm");
				strSH.BackColor = System.Drawing.Color.Yellow;
				errors += "Ora de inceput nu a fost introdusa in formatul standard. Valoarea a fost modificata corespunzator; este inca corecta?";
				inputState = false;
			}
			else
				strSH.BackColor = System.Drawing.Color.White;
		}
		else
		{
			strSH.BackColor = System.Drawing.Color.Pink;
			errors += "Ora de inceput nu a putut fi preluata!"; //"Valoarea este invalida."?
			inputState = false;
		}
		if (DateTime.TryParse(strED.Text, out dummy))
		{
			if (!strED.Text.Equals(dummy.ToString(CalendarExtenderStart.Format)))
			{
				strED.Text = dummy.ToString(CalendarExtenderStart.Format);
				strED.BackColor = System.Drawing.Color.Yellow;
				errors += "Data de final nu a fost introdusa in formatul standard. Valoarea a fost modificata corespunzator; este inca corecta?";
				inputState = false;
			}
			else
				strED.BackColor = System.Drawing.Color.White;
		}
		else
		{
			strED.BackColor = System.Drawing.Color.Pink;
			errors += "Data de final nu a putut fi preluata!"; //"Valoarea este invalida."?
			inputState = false;
		}
		if (DateTime.TryParse(strEH.Text, out dummy))
		{
			if (!strEH.Text.Equals(dummy.ToString("HH:mm")))
			{
				strEH.Text = dummy.ToString("HH:mm");
				strEH.BackColor = System.Drawing.Color.Yellow;
				errors += "Ora de final nu a fost introdusa in formatul standard. Valoarea a fost modificata corespunzator; este inca corecta?";
				inputState = false;
			}
			else
				strEH.BackColor = System.Drawing.Color.White;
		}
		else
		{
			strEH.BackColor = System.Drawing.Color.Pink;
			errors += "Ora de final nu a putut fi preluata!"; //"Valoarea este invalida."?
			inputState = false;
		}
		if (errors.Length > 0)
			this.Master.SetMessage(errors, true);
		return inputState;
	}

	protected void viewBalanta_Click(object sender, EventArgs e)
	{
		if (ValidateInput())
			return; //TODO: stop cross-page post-back
		string state = strSection.SelectedValue + '|' + strSD.Text + ' ' + strSH.Text + '|' + strED.Text + ' ' + strEH.Text;
		//lblSection.Text = state + "<br/>" + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(state.ToCharArray()));
	}
}
